package cn.jy.map.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.ObjectMapper;

import cn.jy.map.domain.AddressMsg;
import cn.jy.map.domain.Communrecord;
import cn.jy.map.domain.Gjzxx;
import cn.jy.map.domain.VillageColligate;
import cn.jy.map.domain.achieve.JCRewardPunishment;
import cn.jy.map.domain.achieve.NurseAchievements;
import cn.jy.map.domain.achieve.NursePostWagesDetail;
import cn.jy.map.service.AchievementsService;
import cn.jy.map.service.MapNewService;
import cn.jy.map.service.VillageColligateService;

/**
 * 地图控制器
 * 
 * @author Administrator
 *
 */
@Controller
public class MapNewController {
	
	@Autowired
	MapNewService mapNewService;
	@Autowired
	private VillageColligateService villageColligateService;
	@Autowired
	private AchievementsService achievementsService;
	
	
	/**
	 * 加载供浆者经纬度基本数据
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("map/updateLcRetComContent")
	public void updateLcRetComContent(HttpServletRequest request, HttpServletResponse response) {
		
		mapNewService.updateLcRetComContent();
		
	}
	/**
	 * 更新供浆者地址经纬度表的所有浆员，将废弃浆员的isDiscarded 是否已废弃字段设置为 1 ，表示已经废弃
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("map/setGjzJwdIsDiscarded")
	public void setGjzJwdIsDiscarded(HttpServletRequest request, HttpServletResponse response) {
		
		mapNewService.setGjzJwdIsDiscarded();
		
	}
	
	/**
	 * 根据姓名、卡号、身份证号，获取供浆者信息
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("map/getGjzxx")
	public @ResponseBody List<Gjzxx> getGjzxx(HttpServletRequest request, HttpServletResponse response) throws Exception {

		response.setContentType("text/html");
		 response.setCharacterEncoding("utf-8");
		
		String code = request.getParameter("code");
		
		if(code != null){
			code = new String(code.getBytes("ISO-8859-1"),"UTF-8"); 
		}
		
		List<Gjzxx> gjzxx = mapNewService.getGjzxx(code);
		
		
		String callback = request.getParameter("callback"); 
		
		if(callback != null && !"".equals(callback)){
			
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(gjzxx);
			
			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = response.getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}
		
		
		return gjzxx;

	}
	/**
	 * 获取浆员的提醒记录，回访记录
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("map/getThData")
	public @ResponseBody Map<String ,List<Communrecord>> getThData(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		
		String providerNo = request.getParameter("providerNo");
		
		
		
		List<Communrecord> comm = mapNewService.getCommunrecord(providerNo);
		List<Communrecord> rem = mapNewService.getRemind(providerNo);
		
		Map<String ,List<Communrecord>> data = new HashMap<String ,List<Communrecord>>();
		data.put("comm", comm);
		data.put("rem", rem);
		
		String callback = request.getParameter("callback"); 
		
		if(callback != null && !"".equals(callback)){
			
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(data);
			
			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = response.getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}
		
		
		return data;
		
	}

	
	
	/**
	 * 获取所有村落【自动标注重点村落】
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("map/getAllAutoKeyVill")
	public @ResponseBody Map<String,List<VillageColligate>>  getAllAutoKeyVill(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		
		
		List<VillageColligate> list = villageColligateService.getAllAutoKeyVill();
		List<VillageColligate> totalList = villageColligateService.getTotalVillageColligate();
		
		Map<String , List<VillageColligate>> map = new HashMap<String , List<VillageColligate>>();
		map.put("allVills", list);
		map.put("totalVills", totalList);
		
		String callback = request.getParameter("callback"); 
		
		if(callback != null && !"".equals(callback)){
			
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(map);
			
			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = response.getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}
		
		
		return 	map;
		
	}
	
	
	
	
	/**
	 * 获取重点村落地址经纬度信息【自动标注重点村落】
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("map/getAutoKeyVillDzJwd")
	public @ResponseBody Map<String,List<AddressMsg>>  getAutoKeyVillDzJwd(HttpServletRequest request, HttpServletResponse response) throws Exception {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		
		Map<String,List<AddressMsg>> map = villageColligateService.getAutoKeyVill();
		
		String callback = request.getParameter("callback"); 
		
		if(callback != null && !"".equals(callback)){
			
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(map);
			
			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = response.getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}
		
		
		return 	map;
		
	}
	/**
	 * 获取所有浆员的综合统计信息
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("map/getAllProviderTj")
	public @ResponseBody VillageColligate  getAllProviderTj(HttpServletRequest request, HttpServletResponse response) throws Exception {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		
		VillageColligate vill = null;
		List<VillageColligate> totalList = villageColligateService.getTotalVillageColligate();
		if(totalList != null && totalList.size()>0){
			vill = totalList.get(0);
		}else{
			vill = new VillageColligate();
		}
		String callback = request.getParameter("callback"); 
		
		if(callback != null && !"".equals(callback)){
			
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(vill);
			
			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = response.getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}
		
		
		return 	vill;
		
	}
	
	/**
	 * 自动设置重点村落
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("map/setAutoKeyVill")
	public void setAutoKeyVill(HttpServletRequest request, HttpServletResponse response) {
		
		villageColligateService.setAutoKeyVillage();
		
	}
	
	/*
	 * --------------绩效---------------------------------
	 */
	/**
	 * 构造所有护士的绩效数据 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("map/loadAchievementsData")
	public void loadAchievementsData(HttpServletRequest request, HttpServletResponse response) {
		
		achievementsService.loadAchievementsData(null,-1);
		
	}
	/**
	 * 构造所有有岗位绩效比例的护士的岗位绩效数据
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("map/loadAchievePostJx")
	public void loadAchievePostJx(HttpServletRequest request, HttpServletResponse response) {
		
		achievementsService.loadAchievePostJx(null,-1);
		
	}
	
	/**
	 * 获取护士综合绩效信息
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("map/getNurseAchievements")
	public @ResponseBody List<NurseAchievements>  getNurseAchievements(HttpServletRequest request, HttpServletResponse response) throws Exception {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		
		String nurseId = request.getParameter("nurseId"); 
		
		List<NurseAchievements> nurseAchievements = achievementsService.getNurseAchievements(nurseId, null);
		
		
		String callback = request.getParameter("callback"); 
		
		if(callback != null && !"".equals(callback)){
			
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(nurseAchievements);
			
			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = response.getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}
		
		
		return 	nurseAchievements;
		
	}
	
	/**
	 * 获取护士某年某月的岗位绩效
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("map/getNursePostWagesDetail")
	public @ResponseBody List<NursePostWagesDetail>  getNursePostWagesDetail(HttpServletRequest request, HttpServletResponse response) throws Exception {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		
		String nurseId = request.getParameter("nurseId"); 
		String month = request.getParameter("month");
		
		int m = -1;
		if(month != null && !"".equals(month)){
			m = Integer.parseInt(month);
		}
		
		List<NursePostWagesDetail> nursePostWagesDetail = achievementsService.getNursePostWagesDetail(nurseId, null, m);
		
		
		String callback = request.getParameter("callback"); 
		
		if(callback != null && !"".equals(callback)){
			
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(nursePostWagesDetail);
			
			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = response.getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}
		
		
		return 	nursePostWagesDetail;
		
	}
	/**
	 * 获取护士某年某月的血源发展绩效
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("map/getXyfz")
	public @ResponseBody List<Map<String, Object>>  getXyfz(HttpServletRequest request, HttpServletResponse response) throws Exception {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		
		String nurseId = request.getParameter("nurseId"); 
		String month = request.getParameter("month");
		
		int m = -1;
		if(month != null && !"".equals(month)){
			m = Integer.parseInt(month);
		}
		
		List<Map<String, Object>> xyfz = achievementsService.getXyfz(nurseId, null, m);
		
		
		String callback = request.getParameter("callback"); 
		
		if(callback != null && !"".equals(callback)){
			
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(xyfz);
			
			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = response.getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}
		
		
		return 	xyfz;
		
	}
	/**
	 * 查询某个护士某年某月的奖惩详情
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("map/getJCRewardPunishment")
	public @ResponseBody List<JCRewardPunishment>  getJCRewardPunishment(HttpServletRequest request, HttpServletResponse response) throws Exception {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		
		String nurseId = request.getParameter("nurseId"); 
		String month = request.getParameter("month");
		
		int m = -1;
		if(month != null && !"".equals(month)){
			m = Integer.parseInt(month);
		}
		
		List<JCRewardPunishment> jc = achievementsService.getJCRewardPunishment(nurseId, null, m);
		
		
		String callback = request.getParameter("callback"); 
		
		if(callback != null && !"".equals(callback)){
			
			ObjectMapper mapper = new ObjectMapper();
			String json = mapper.writeValueAsString(jc);
			
			String retStr = callback + "(" + json + ")";
			try {
				PrintWriter out = response.getWriter();
				out.print(retStr);
				out.flush();
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return null;
		}
		
		
		return 	jc;
		
	}
	
	
	
	
	
}
